package com.itheima.service.impl;

import com.itheima.common.utils.DateUtils;
import com.itheima.mapper.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public List<OrderSetting> getOrderSettingByMonth(String month){
        //先将月份字符串转换成日期对象
        try {
            Date date = DateUtils.parseString2Date(month, "yyyy-MM");
            //使用Calendar类处理日期
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            //根据calendar年月补全日期对象 startDay(对应该月第一天) endDay(对应该月最后一天)
            LocalDate startDay = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, 1);
            LocalDate endDay = startDay.withDayOfMonth(startDay.lengthOfMonth());
            //将startDay和endDay作为参数，调用dao层方法，查询对应的预约设置数据
            return orderSettingDao.getOrderSettingByMonth(startDay, endDay);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        //修改日期格式 转为yyyy-MM-dd
        try {
            String orderDate = DateUtils.parseDate2String(orderSetting.getOrderDate(), "yyyy-MM-dd");
            orderSetting.setOrderDate(DateUtils.parseString2Date(orderDate));
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean uploadExcel(MultipartFile excelFile) {
        try {
            // 解析Excel文件并保存数据
            List<OrderSetting> orderSettings = parseExcel(excelFile);

            // 保存数据到数据库
            orderSettingDao.saveAll(orderSettings);

            return true;
        } catch (IOException e) {
            return false;
        }
    }

    private List<OrderSetting> parseExcel(MultipartFile excelFile) throws IOException {
        List<OrderSetting> orderSettings = new ArrayList<>();
        try (InputStream inputStream = excelFile.getInputStream()) {
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            for (Row row : sheet) {
                if (row.getRowNum() == 0) continue; // 跳过表头
                OrderSetting orderSetting = new OrderSetting();
                orderSetting.setOrderDate(row.getCell(0).getDateCellValue());
                orderSetting.setNumber((int) row.getCell(1).getNumericCellValue());
                orderSettings.add(orderSetting);
            }
        } catch (Exception e) {
            throw new IOException("Excel文件格式不兼容或已损坏", e);
        }
        return orderSettings;
    }

}
